package com.zmc.order.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.order.infrastructure.bean.query.ReturnApplyQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;
import com.zmc.order.infrastructure.dao.OrderReturnApplyDao;
import com.zmc.order.infrastructure.entity.OrderReturnApplyEntity;
import com.zmc.order.infrastructure.service.OrderReturnApplyService;


/**
 * @author zmc
 */
@Service("orderReturnApplyService")
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyDao, OrderReturnApplyEntity> implements OrderReturnApplyService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public PageUtils queryPage(ReturnApplyQuery query) {
        return new PageUtils(baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()),getWrapper(query)));
    }

    @Override
    public OrderReturnApplyEntity getByOrderSn(String orderSn) {
        LambdaQueryWrapper<OrderReturnApplyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderReturnApplyEntity::getOrderSn, orderSn);
        return getOne(wrapper);
    }

    private LambdaQueryWrapper<OrderReturnApplyEntity> getWrapper(ReturnApplyQuery query) {
        LambdaQueryWrapper<OrderReturnApplyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderReturnApplyEntity::getMerchantId, authTokenService.getMemberInfo().getMemberId())
                .eq(!StringUtils.isEmpty(query.getOrderSn()), OrderReturnApplyEntity::getOrderSn, query.getOrderSn())
                .eq(!Objects.isNull(query.getStatus()), OrderReturnApplyEntity::getStatus, query.getStatus())
                .orderByDesc(OrderReturnApplyEntity::getCreateTime);
        return wrapper;
    }

}